//
//  NewsViewController.m
//  HuiWenMovie
//
//  Created by niceDay on 15/7/17.
//  Copyright (c) 2015年 computer. All rights reserved.
//

#import "NewsViewController.h"
#import "NewsCell.h"
#import "DataService.h"
#import "UIViewExt.h"
#import "newsImageViewController.h"
#import "common.h"
#import "newsDetailViewController.h"
@interface NewsViewController ()
{
    UIImageView *headerImageView;
    UILabel *headerLable;
}
@end

@implementation NewsViewController

#pragma mark - mainViewcontroller



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.navigationItem.title = @"新闻";
    }
    return self;
}

- (void)viewDidLoad {

    [super viewDidLoad];
    
    self.navigationController.delegate =self;
    [self _createTableView];
    [self _createHeaderView];
    [self _loadData];
    
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - createSubview

- (void)_createHeaderView{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 200)];
    headerView.backgroundColor = [UIColor clearColor];
    _tableView.tableHeaderView = headerView;
    
    headerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, Kwidth,200)];
    headerLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, Kwidth, 30)];
    headerLable.bottom = headerImageView.bottom;
    headerLable.backgroundColor = [UIColor grayColor];
   
    [self.view addSubview:headerImageView];
    [self.view addSubview:headerLable];
    
}

- (void)_createTableView{
    self.navigationController.navigationBar.translucent = YES;
    self.tabBarController.tabBar.translucent = YES;
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Kwidth, Kheight) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor blackColor];
    UINib *nib = [UINib nibWithNibName:@"NewsCell" bundle:[NSBundle mainBundle]];
    [_tableView registerNib:nib forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
    
}

#pragma mark - navigationController delegate

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
      CGRect frame = self.tabBarController.tabBar.frame ;
    
    if (navigationController.viewControllers.count == 1) {
       self.tabBarController.tabBar.hidden = NO;
        frame.origin.x = 0;
        self.tabBarController.tabBar.frame = frame;

    }
    else{
       // self.tabBarController.tabBar.hidden = YES;
        frame.origin.x = -Kwidth;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:.2];
        self.tabBarController.tabBar.frame = frame;
        [UIView commitAnimations];
    }
}


#pragma mark - tableView delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [_modalArray count];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NewsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    NewsModal *modal = _modalArray[indexPath.row];
    cell.modal = modal;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  90;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offY = scrollView.contentOffset.y;
    if (offY > -64) {//向上
        headerImageView.top = - offY;
        headerLable.bottom = headerImageView.bottom;
    }
    else{
        CGFloat newHeight = ABS(offY) -64+200;
        CGFloat newWidth = newHeight/200*Kwidth;
        headerImageView.frame = CGRectMake((Kwidth-newWidth)/2, 64, newWidth,newHeight);
        headerLable.bottom = headerImageView.bottom;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NewsModal *modal = _modalArray[indexPath.row];
    
    if (modal.type == 0) {
        newsDetailViewController *vc = [[newsDetailViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else{
    newsImageViewController *vc = [[newsImageViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    }
    
}

#pragma mark - loadData

- (void)_loadData{
    _modalArray = [[NSMutableArray alloc] init];
    
    
    NSArray *array = [DataService initWitnJsionFileName:newsJson];
    
//    for (int i = 0; i < 15; i ++) {
//        NSDictionary *dic = array[i];
//        NewsModal *modal = [[NewsModal alloc] init];       
//        [modal setValuesForKeysWithDictionary:dic];
//        modal.title = dic[@"title"];
//        modal.summary = dic[@"summary"];
//        modal.image = dic[@"image"];
//        NSNumber *num = dic[@"type"];
//        modal.type = [num intValue];
//        [_modalArray addObject:modal];
//   }
    
    for (NSDictionary *dic in array) {
        NewsModal *modal = [[NewsModal alloc] init];
        [modal setValuesForKeysWithDictionary:dic];
        [_modalArray addObject:modal];
    }
    
    NewsModal *firstModal = _modalArray[0];
    NSString *str =firstModal.image;
    [headerImageView sd_setImageWithURL:[NSURL URLWithString:str]];
    headerLable.text = firstModal.title;
    [_modalArray removeObjectAtIndex:0];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
